/* 
//	3DCreateSprite
*/
#include "Brick.h"
#include "gfx/all_gfx.c"
#include "MathAndPhysics.h"
#include <PA9.h>
#include <list>

// Function: main()
int main(int argc, char ** argv)
{
	PA_Init();    // Initializes PA_Lib
	PA_InitVBL(); // Initializes a standard VBL
	
	PA_Init3D(); // Uses Bg0
	PA_Reset3DSprites();
	
	/*std::list<Brick *> brickList = new std::list<Brick*>();
	brickList->push_back(aBrick);*/
	Brick * aBrick = new Brick((void*)mollusk_Texture,16,16,TEX_16BITS, 0,128,96);
	//Brick * aSecondBrick = new Brick((void*)mollusk_Texture,16,16,TEX_16BITS, 0,180,96);
	
	// Initialise the text system on the top screen
	PA_InitText(0, 1);
	PA_InitText(1, 1);  // Initialise the text system on the top screen	
	
	//Variable
	struct tm now;
	struct tm nowPrev;
	struct tm nowPrevPrev;
	int timeElapsed = 0;			
	bool previouslyHeld = false;
	Vector2D previousStylusPosition = Vector2D(0,0);
	Vector2D stylusPosition = Vector2D(0,0);
	Vector2D stylusVelocity = Vector2D(0,0);
	//PA_RTC.Minutes, .Hour, .Seconds, .Day, .Month, and .Year 
	while(1)
	{
		
		nowPrevPrev.tm_hour = nowPrev.tm_hour;
		nowPrevPrev.tm_min = nowPrev.tm_min;
		nowPrevPrev.tm_mon = nowPrev.tm_mon;
		nowPrevPrev.tm_sec = nowPrev.tm_sec;
		nowPrevPrev.tm_year = nowPrev.tm_year;
		nowPrevPrev.tm_mday = nowPrev.tm_mday;
		
		nowPrev.tm_hour = now.tm_hour;
		nowPrev.tm_min = now.tm_min;
		nowPrev.tm_mon = now.tm_mon;
		nowPrev.tm_sec = now.tm_sec;
		nowPrev.tm_year = now.tm_year;
		nowPrev.tm_mday = now.tm_mday;
		
		now.tm_hour = PA_RTC.Hour;
		now.tm_min = PA_RTC.Minutes;
		now.tm_mon = PA_RTC.Month;
		now.tm_sec = PA_RTC.Seconds;
		now.tm_year = PA_RTC.Year;
		now.tm_mday = PA_RTC.Day;
		
		float dt = difftime(mktime(&now),mktime(&nowPrev));
		float dtMinus1 = difftime(mktime(&nowPrev),mktime(&nowPrevPrev));/**/

		stylusPosition = Vector2D(Stylus.X,Stylus.Y);
		//stylusVelocity = (stylusPosition-previousStylusPosition)*(1/dtMinus1);
		stylusVelocity = (stylusPosition-previousStylusPosition);
		//aBrick->tick(1,2,stylusPosition,stylusVelocity,Stylus.Held,previouslyHeld);/**/
		//aBrick->tick(dt,dtMinus1,stylusPosition,stylusVelocity,Stylus.Held,previouslyHeld);/**/
		
		aBrick->tick(stylusPosition,stylusVelocity,Stylus.Held,previouslyHeld);/**/
		//aSecondBrick->tick(stylusPosition,stylusVelocity,Stylus.Held,previouslyHeld);
		
		//aBrick->collidesWith(aSecondBrick->getPosition().x-aSecondBrick->getWidth()/2,aSecondBrick->getPosition().x-aSecondBrick->getHeight()/2,aSecondBrick->getWidth(),aSecondBrick->getHeight());
		//aSecondBrick->collidesWith(aBrick->getPosition().x-aBrick->getWidth()/2,aBrick->getPosition().x-aBrick->getHeight()/2,aBrick->getWidth(),aBrick->getHeight());/**/
		
		float restitution = 1;
		
		previouslyHeld = Stylus.Held;
		previousStylusPosition = Vector2D(Stylus.X,Stylus.Y);

		
		PA_3DProcess();
		PA_WaitForVBL();
	}
	return 0;
}